package com.example.figurehecheng.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.domain.dto.ProgramDTO;
import com.example.figurehecheng.domain.dto.ProgramPageDTO;
import com.example.figurehecheng.domain.entity.Program;
import com.example.figurehecheng.mapper.ProgramMapper;
import com.example.figurehecheng.service.IProgramService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
@RequiredArgsConstructor
public class ProgramServiceImpl extends ServiceImpl<ProgramMapper, Program> implements IProgramService {

    private final ProgramMapper programMapper;

    /**
     * 分页查询项目信息
     * @param programPageDTO
     * @return
     */
    @Override
    public PageResult selectProgramPage(ProgramPageDTO programPageDTO) {
        Page<Program> programPage = new Page<>( programPageDTO.getPageNum(), programPageDTO.getPageSize());
        LambdaQueryWrapper<Program> eq = new LambdaQueryWrapper<Program>()
                .like(StrUtil.isNotBlank(programPageDTO.getName()), Program::getName, programPageDTO.getName())
                .eq(Program::getStadiumId, BaseContext.getCurrentId());
        Page<Program> page = programMapper.selectPage(programPage, eq);
        long total = page.getTotal();
        List<Program> records = page.getRecords();
        return new PageResult(total, records);
    }

    /**
     * 添加项目信息
     * @param programDTO
     * @return
     */
    @Override
    public void saveHouse(ProgramDTO programDTO) {
        Program program = new Program();
        BeanUtils.copyProperties(programDTO, program);
        program.setStadiumId(Math.toIntExact(BaseContext.getCurrentId()));
        save(program);
    }

    /**
     * 修改项目
     * @param programDTO
     * @return
     */
    @Override
    public void updateProgram(ProgramDTO programDTO) {
        Program program = new Program();
        BeanUtils.copyProperties(programDTO, program);
        updateById(program);
    }

    /**
     * 根据id删除项目
     * @param id
     * @return
     */
    @Override
    public void deleteProgram(Integer id) {
        removeById(id);
    }
}
